package leetcode.力扣竞赛.第327场周赛;

/**
 * @author: yan
 * @description:正整数和负整数的最大计数
 * @create: 2023-02-22 15:54
 **/

/**
 * tips:二分查找小于某值的第一个数时，只需要在二分的基础上把等于mid的判断去掉
 */
public class Solution2529 {
    public static void main(String[] args) {
        int[] arr = new int[]{-3,-2,-1,0,0,1,2};
        System.out.println(maximumCount(arr));
    }

    public static int maximumCount(int[] nums) {
        int res = 0;
        int left = 0, right = nums.length - 1;
        if (nums[right] < 0 || nums[left] > 0) {
            return nums.length;
        }
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (nums[mid] < 0)
                left = mid + 1;
            else
                right = mid - 1;
        }
        res = left;
        left = 0;
        right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (nums[mid] > 0)
                right = mid - 1;
            else
                left = mid + 1;
        }
        return Math.max(res, nums.length - right - 1);
    }
}
